package com.book.test;

import com.book.entity.Account;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author SuperYu  2024/8/8
 */
@SpringBootTest
public class SystemApplicationTest2 {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Test
    public void testRedisString() throws JsonProcessingException {
        Account account = new Account();
        account.setUserName("张三");
        account.setPassword("123456");

        // 因为StringRedisTemplate只能操作字符串，
        // 所以我们需要把java对象转换成json字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(account);
        // 写入到redis中
        // redisTemplate.opsForValue().set("account", json );

        // 从redis中取出数据:是一个json字符串
        String temp = redisTemplate.opsForValue().get("account");
        // 把json字符串，再转换成java对象
        // Account a = objectMapper.readValue(temp, Account.class);

        JavaType javaType = objectMapper.getTypeFactory().constructType(Account.class);
        Object a = objectMapper.readValue(temp, javaType);

        System.out.println(a);
    }

    @Test
    public void testRedisStringList() throws JsonProcessingException {
        ArrayList<Account> list = new ArrayList<>();
        list.add(new Account("aaa","123"));
        list.add(new Account("bbb","123"));
        list.add(new Account("ccc","123"));
        // 因为StringRedisTemplate只能操作字符串，
        // 所以我们需要把java对象转换成json字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonList = objectMapper.writeValueAsString(list);
        // 写入到redis中
        // redisTemplate.opsForValue().set("accountList", jsonList );

        // 从redis中取出数据
        String tempList = redisTemplate.opsForValue().get("accountList");
        // 把json字符串，再转换成java对象
        // ArrayList list2 = objectMapper.readValue(tempList, ArrayList.class);


        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Account.class);
        List<Account> list2 = (List<Account>)objectMapper.readValue(tempList, javaType);

        for (Object o : list2) {
            System.out.println(o);
        }

    }



    @Test
    public void testRedisList(){

        // 写入到redis中
        // redisTemplate.opsForList().leftPushAll("city", "上海","北京","广州");

        // 从redis中取出数据
        List<String> city = redisTemplate.opsForList().range("city", 0, -1);
        for (String o : city) {
            System.out.println(o);
        }
    }

    @Test
    public void testRedisSet(){
        // 写入到redis中
        redisTemplate.opsForSet().add("human","jack");

        // 从redis中取出数据
        Set<String> set = redisTemplate.opsForSet().members("human");
        set.forEach(System.out::println);

        // 判断指定的值，是否存在于set中
        Boolean f = redisTemplate.opsForSet().isMember("human", "tom");
        System.out.println(f);
    }

    @Test
    public void testRedisZSet(){
        // 写入到redis中
        redisTemplate.opsForZSet().add("humans","jack",2);
        redisTemplate.opsForZSet().add("humans","张三",1);
        redisTemplate.opsForZSet().add("humans","王五",4);
        redisTemplate.opsForZSet().add("humans","rose",3);

        // 从redis中取出数据
        Set<String> set = redisTemplate.opsForZSet().range("humans", 0, -1);
        for (String o : set) {
            System.out.println(o);
        }
    }

    @Test
    public void testRedisMap(){
        HashMap<Object, Object> map = new HashMap<>();
        map.put("name","jack");
        map.put("age","22");
        map.put("gender","male");

        // 写入到redis中
        redisTemplate.opsForHash().putAll("stu", map);

        // 从redis中读出数据-- 读取单个值
        // Object gender = redisTemplate.opsForHash().get("stu", "gender");
        // System.out.println(gender);
        //
        // System.out.println("========================================");
        // // 从redis中读出数据-- 读取多个值
        // ArrayList list = new ArrayList();
        // list.add("name");
        // list.add("age");
        // list.add("gender");
        // list.add("human");
        //
        // List stu = redisTemplate.opsForHash().multiGet("stu", list);
        // stu.forEach(System.out::println);

    }


}
